# -*- coding: utf8-*-
from datetime import datetime
from app import db

_author_ = 'yykrlc'


def db_save(dbs, bing=None):
    try:
        db.session.add(dbs)
        db.session.commit()
        db.session.close()
        return True
    except Exception as e:
        print e
        return False


def db_delete(dbs, bing=None):
    db.session.delete(dbs)
    db.session.commit()
    db.session.close()


def convert_to_dict(obj):
    """把Object对象转换成Dict对象"""
    if obj is None:
        return {}
    dict_ = {}
    dict_.update(obj.__dict__)
    try:
        dict_.__delitem__("_sa_instance_state")
    except:
        pass
    for i in dir(obj):  # 把自定义图片地址加入字典
        if i.endswith("_path"):
            dict_.update({i: eval("obj.%s" % i)})
    for j in dict_:
        if dict_[j] in ["None", None]:
            dict_[j] = ""
    return dict_


class Messages(db.Model):
    __tablename__ = 'wx_messages'
    """微信公众号的聊天记录"""
    id = db.Column(db.BigInteger, primary_key=True, nullable=False)
    source = db.Column(db.String(16))
    message_id = db.Column(db.String(32))
    time = db.Column(db.BIGINT)
    types = db.Column(db.String(16))
    target = db.Column(db.String(32))
    content = db.Column(db.TEXT)
    oid = db.Column(db.BigInteger)
    created_at = db.Column(db.DateTime, default=datetime.now)
    
    def __repr__(self):
        return '<user_id %r>' % self.id


class WxUserInfo(db.Model):
    __tablename__ = 'wx_user_info'
    """用户信息"""
    id = db.Column(db.BigInteger, primary_key=True, nullable=False)
    openid = db.Column(db.String(32))
    unionid = db.Column(db.String(32))
    nickname = db.Column(db.String(32))
    headimgurl = db.Column(db.String(256))
    province = db.Column(db.String(16))
    city = db.Column(db.String(16))
    sex = db.Column(db.SMALLINT)
    oid = db.Column(db.BigInteger)
    created_at = db.Column(db.DateTime, default=datetime.now)
    
    def __repr__(self):
        return '<wx_user_info_id %r>' % self.id
        
        
class WxGzh(db.Model):
    __tablename__ = 'wx_gongzhonghao'
    """微信公众号信息"""
    id = db.Column(db.BigInteger, primary_key=True, nullable=False)
    appid = db.Column(db.String(32))
    target = db.Column(db.String(16))
    name = db.Column(db.String(32))
    qr_code = db.Column(db.TEXT)
    qr_code2 = db.Column(db.String(500))
    principal_name = db.Column(db.String(256))
    head_img = db.Column(db.String(500))
    oid = db.Column(db.BigInteger)
    created_at = db.Column(db.DateTime, default=datetime.now)
    
    def __repr__(self):
        return '<WxGzh_id %r>' % self.id


class WxRoomOfGzh(db.Model):
    __tablename__ = 'wx_room_of_gzh'
    """微信公众号和房间对应关系,id对应"""
    id = db.Column(db.BigInteger, primary_key=True, nullable=False)
    room_id = db.Column(db.String(32))
    gzh_id = db.Column(db.String(16))
    oid = db.Column(db.BigInteger)
    created_at = db.Column(db.DateTime, default=datetime.now)
    
    def __repr__(self):
        return '<WxRoomOfGzh_id %r>' % self.id
    
    
class StatGroups(db.Model):
    __teblename__ = 'stat_grpups'
    """自定义操作-分组统计"""
    id = db.Column(db.Integer, primary_key=True)
    group_id = db.Column(db.Integer, nullable=False)
    oid = db.Column(db.Integer, nullable=True)
    unit_id = db.Column(db.String(100), nullable=True)
    read_num = db.Column(db.Integer, nullable=False, server_default=db.text("'0'"))
    write_num = db.Column(db.Integer, nullable=False, server_default=db.text("'0'"))
    communicate_num = db.Column(db.Integer, nullable=False, server_default=db.text("'0'"))
    created_at = db.Column(db.TIMESTAMP, nullable=False, server_default=db.text("CURRENT_TIMESTAMP"))
    updated_at = db.Column(db.TIMESTAMP, nullable=False, server_default=db.text("CURRENT_TIMESTAMP"))